package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Author: 笨蛋超人不会飞
 * Date:  17:17
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category=new Category();
        //使用属性复制的方式
        BeanUtils.copyProperties(categoryDTO,category);
        //设置剩余的属性
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());
        //statue属性默认是禁用的
        category.setStatus(0);
        //调用mapper
        categoryMapper.insert(category);

    }

    @Override
    public PageResult getAllCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
        //使用分页pagehelp
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        //调用mapper
        Page<Category> page=categoryMapper.selectAllCategory(categoryPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteCategoryById(Long id) {
        categoryMapper.deleteCategoryById(id);
    }

    @Override
    public void updateCategoryStatus(Integer status, Long id) {
        Category category=new Category();
        category.setId(id);
        category.setStatus(status);
        //设置更新的时间和更新的人
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);

    }

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);


    }

    @Override
    public List<Category> getCategoryByType(Integer type) {
        return categoryMapper.selectCategoryByType(type);
    }

    @Override
    public List<Category> getlist(Integer type) {
        return categoryMapper.list(type);
    }




}
